package it.tcm.TeacherClassMate.action;

import it.tcm.TeacherClassMate.beans.SessionUser;
import it.tcm.TeacherClassMate.util.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;



public class LoginAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods


	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * 
	 */

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) 
	{		
		 logger.info("LOGIN");
		 		 
		 SessionUser sessionUser = (SessionUser) request.getSession().getAttribute("sessionUser");
		 if (sessionUser != null) {
			    String username = sessionUser.getUsername();
			    logger.info("username found " + username);
			    if(sessionUser.getAdministrator() != null && sessionUser.getAdministrator().equals(Util.ADMINISTRATOR)) {
					 return mapping.findForward("amnilogged"); 
				} else {
					 return mapping.findForward("logged"); 
				} 
		 }
		 return mapping.findForward("showLogin"); 
		 
	}
	
	
}